<?php
header('Content-Type:text/html;charset=utf-8');
define('MINI_RUN', true);
require ('connect.php');
if (!isset($_POST['op'])){
	exit();
}
checklogin();
$op=$_POST['op'];

switch ($op){
	case 'getclassleft':  //获取栏目左侧列表
		if (isset($_POST['classid'])){
			echo getclassdata($_POST['classid'], 'LeftList');
		}
		break;
	case 'getclassright':  //获取栏目右侧列表
		if (isset($_POST['classid'])){
			echo getclassdata($_POST['classid'], 'RightList');
		}
		break;
	case 'getclasscontent':  //获取栏目首页模板
		if (isset($_POST['classid'])){
			echo getclassdata($_POST['classid'], 'Content');
		}
		break;
	case 'getpagetemp':  //获取文章内容模板
		if (isset($_POST['classid'])){
			echo getpagetemp($_POST['classid']);
		}
		break;
	case 'checkexists':  //检测文件名是否已存在
		if (isset($_POST['classid'])&&isset($_POST['filename'])){
			echo checkfilename($_POST['filename'], $_POST['classid']);
		}
		break;
	case 'getpagelist':
		if (isset($_POST['classid'])&&isset($_POST['pagesize'])&&isset($_POST['pageindex'])){
			echo getpagelist($_POST['classid'],$_POST['pagesize'],$_POST['pageindex']);
		}
		break;
	case 'getpageleft':
		if (isset($_POST['classid'])){
			echo getcourse($_POST['classid'], 'LeftSub');
		}
		break;
	case 'getpageright':
		if (isset($_POST['classid'])){
			echo getcourse($_POST['classid'], 'RightSub');
		}
		break;
	default:
		echo 'Ajax参数出错！';							
}
db_close();

/**
 * 获取栏目首页的单一数据列
 * @param $id int
 * 栏目ID
 * @param $part string
 * 列名
 */
function getclassdata($id,$part){
	$data='未能获取到数据。';
	if (empty($part)){
		db_close();
		$data='column参数不足！';
		return $data;
	}
	global $link;
	$stmt=$link->prepare('SELECT '.$part.' FROM Mini_Class WHERE ClassId=? limit 1');
	$stmt->bind_param('i', $id);
	$stmt->execute();
	$stmt->bind_result($var1);
	if ($stmt->fetch()){
		$data=$var1;
	}
	$stmt->close();
	return $data;	
}

/**
 * 获取栏目的文章内容模板
 * @param $id
 */
function getpagetemp($id){
	global $link;
	$temp='未能获取到数据。';
	$stmt=$link->prepare('SELECT TempContent FROM Mini_Temp WHERE ClassId=? limit 1');
	$stmt->bind_param('i', $id);
	$stmt->execute();
	$stmt->bind_result($var1);
	if ($stmt->fetch()){
		$temp=$var1;
	}
	$stmt->close();
	return $temp;
}


function checkfilename($filename,$classid){
	if (empty($filename)){
		return true;
	}
	global $link;
	$bool=false;
	$stmt=$link->prepare('SELECT PageId FROM Mini_Page WHERE ClassId=? AND FileName=? limit 1');
	$stmt->bind_param('is', $classid,$filename);
	$stmt->execute();
	if ($stmt->num_rows()){
		$bool=true;		
	}
	$stmt->close();	
	return $bool;
}
function getpagelist($classid,$pagesize,$pageindex){
	global $link,$mini;
	$start_p=($pageindex-1)*$pagesize;
	$rs_str='';
	$str='SELECT PageId,Title,PageUrl,UserName,FileName FROM Mini_Page WHERE ClassId=? limit '.$start_p.','.$pagesize;
	if($classid==0){
		$str='SELECT PageId,Title,PageUrl,UserName,FileName FROM Mini_Page limit '.$start_p.','.$pagesize;
	}	
	$stmt=$link->prepare($str);
	if ($classid>0){
		$stmt->bind_param('i', $classid);
	}
	$stmt->execute();
	$stmt->bind_result($var1,$var2,$var3,$var4,$var5);
	while ($stmt->fetch()) {
		$rs_str.='<tr><td align="center">'.$var1.'</td><td align="center"><a href="http://'.$mini['url'].$var3.$var5.'.html" target="_blank">'.$var2.'</a></td><td align="center">'.$var4.'</td><td align="center">暂无</td><td align="center"><a href="update.php?pageid='.$var1.'">修改</a>&nbsp;<a href="delete.php?pageid='.$var1.'">删除</a></td></tr>';
	}
	$stmt->close();
	return $rs_str;
}

function getcourse($classid,$part){
	global $link;	
	$courselist='未能获取数据';
	$stmt=$link->prepare('SELECT '.$part.' FROM Mini_Course WHERE ClassId=? limit 1');
	$stmt->bind_param('i', $classid);
	$stmt->execute();
	$stmt->bind_result($var1);
	if ($stmt->fetch()){
		$courselist=$var1;
	}
	$stmt->close();
	return $courselist;
}
?>